package cn.com.cetccst.appstore.database;


import android.content.Context;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.lifecycle.LiveData;
import androidx.room.Room;

import java.util.List;

import cn.com.cetccst.appstore.database.dao.AppModelDao;
import cn.com.cetccst.appstore.database.dao.CategoryModelDao;
import cn.com.cetccst.appstore.database.model.AppModel;
import cn.com.cetccst.appstore.database.model.CategoryModel;

public class AppStoreRepository {
    private AppStoreDatabase appDatabase;
    private static AppStoreRepository instance;
    private final CategoryModelDao categoryModelDao;
    private final AppModelDao appModelDao;

    private AppStoreRepository(Context context) {
        appDatabase = Room.databaseBuilder(context, AppStoreDatabase.class, "appstore.db")
                .allowMainThreadQueries()
//                .addMigrations(MIGRATION_1_2)
                .build();
        categoryModelDao = appDatabase.geCategoryModelDao();
        appModelDao = appDatabase.getAppModelDao();
    }

    public static void initialize(Context context) {
        instance = new AppStoreRepository(context);
    }

    public static AppStoreRepository getInstance() {
        return instance;
    }

    public void saveAppModel(List<AppModel> appModelList) {
        appDatabase.runInTransaction(() -> {
            appModelDao.clearAll();
            appModelDao.insertAll(appModelList);
        });

    }

    public void saveAppModel(AppModel appModel) {
        appModelDao.insert(appModel);
    }

    public void updateAppModel(AppModel appModel) {
        appModelDao.update(appModel);
    }

    public LiveData<AppModel> loadAppByAppId(String appId) {
        return appModelDao.loadAppByAppId(appId);
    }

    public LiveData<List<AppModel>> loadAllApps(String categoryId, String searchKey) {
        String category = processCategory(categoryId);
        String search = processSearch(searchKey);
        return appModelDao.loadAllApps(category, search);
    }

    public LiveData<List<AppModel>> loadAllNeedUpgradeApps(String searchKey) {
        return appModelDao.loadAllNeedUpgradeApps();
    }

    @NonNull
    private static String processCategory(String category) {
        if (TextUtils.isEmpty(category) || TextUtils.isEmpty(category.trim())) {
            return "%%";
        }
        category = category.trim().replaceAll("'", "''");
        return "%\"" + category + "\"%";
    }

    @NonNull
    private static String processSearch(String query) {
        if (TextUtils.isEmpty(query)) {
            return "%%";
        }
        if (TextUtils.isEmpty(query.trim())) {
            query = query.trim();
        }
        query = query.replaceAll("'", "''");
        return "%" + query + "%";
    }

    public void saveCategoryModel(List<CategoryModel> categoryList) {
        appDatabase.runInTransaction(() -> {
            categoryModelDao.clearAll();
            categoryModelDao.insertAll(categoryList);
        });
    }

    public LiveData<List<CategoryModel>> loadAllCategory() {
        return categoryModelDao.loadAllCategory();
    }

    public LiveData<Integer> loadCountNeedUpgradeApps() {
        return appModelDao.countNeedUpgradeApps();
    }

    public AppModel getAppByAppId(String appId) {
        return appModelDao.getAppByAppId(appId);
    }

    public AppModel getAppByPackageName(String packageName) {
        return appModelDao.getAppByPackageName(packageName);
    }

    public LiveData<AppModel> loadAppByPackageName(String packageName) {
        return appModelDao.loadAppByPackageName(packageName);
    }
}
